﻿package Born.utils
{
	import flash.display.*;
	import flash.events.*;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.text.*;
	import flash.utils.*;


	public class KeyBoardUI extends MovieClip 
	{
		var keyboardMc:MovieClip;

		var capsOn:Boolean = false;
		var capsOnShift:Boolean = false;
		var numSwitch:Boolean = false;

		var arrayLength:Number;

		var txtArray_array:Array = new Array();
		var lineLength_array:Array = new Array();
		var char_array:Array = new Array();
		var keyType:String;
		var currentTF;


		// standard keys name array from movieclip 'key1' till 'key26'
		var abcName_array:Array = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26");

		// numpad keys array from movieclip 'numpad0' till 'numpad9'
		var numpad_array:Array = new Array("0","1","2","3","4","5","6","7","8","9");

		// special keys (comma, slash question mark etc.) array from movieclip 'keyX0' till 'keyX7'
		var specName_array:Array = new Array("1","2","3","4","5","6","7");
		var specChar_array:Array = new Array(":",";",",",".","/","?","-");

		// number keys name array from movieclip 'num1' till 'num10'
		var numName_array:Array = new Array("1","2","3","4","5","6","7","8","9","10");
		// 'numChar1_array' is the default array that is showed
		var numChar1_array:Array = new Array("1","2","3","4","5","6","7","8","9","0");
		// 'numChar2_array' is showed when the user pressed the Capslock or the Shift key
		var numChar2_array:Array = new Array("!","@","#","$","%","^","&","*","(",")","_");
		
		var isSound:Boolean;
		var clickSound:Sound = new Sound();
		var backSound:Sound = new Sound();
		var spaceSound:Sound = new Sound();


		// KeyBoard constructor
		public function KeyBoardUI(_keyboardMc, _keyType) 
		{
			trace("KeyBoard constructor");

			char_array["abc"] = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
			char_array["qwerty"] = new Array("Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M");

			keyboardMc = _keyboardMc;
			keyType = _keyType;
			setAbcArray(capsOn);
			setNumKeys(numSwitch);
			setNumpadArray();
			setSpecialKeys();
			addSpecialKeys();
			switchCapsLock();
			capsLockOff();

		}

		// set the currentTF variable to the active textfield
		public function setTextFocus(tfName) 
		{
			if (tfName.type == "input") 
			{
				currentTF = tfName;
				trace("textfield focus = "+currentTF.name);
				currentTF.setSelection(currentTF.length,currentTF.length);
			}
		}

		// add onPress event and set the textfield characters in the movieclip
		private function setAbcArray(capsBool) 
		{
			for (var i = 0; i < abcName_array.length; i++) 
			{
				var dyn = keyboardMc["key" + abcName_array[i]];
				dyn.btn.gotoAndStop(1);

				var tempChar:String = "";
				var tChar:String = char_array[keyType][i];

				if (capsBool) {
					tempChar = tChar.toUpperCase();
				} else {
					tempChar = tChar.toLowerCase();
				}
				dyn.cTF.text = tempChar;
				dyn.charVal = tempChar;
				dyn.cTF.mouseEnabled = false;

				dyn.addEventListener(MouseEvent.MOUSE_DOWN , abcFnc);
			}
		}
		private function keyROver(e:MouseEvent) {
			e.target.btn.gotoAndStop(2);
		}

		private function keyROut(e:MouseEvent) {
			e.target.btn.gotoAndStop(1);
		}
		
		public function addSound(soundClick:String, soundBack:String, soundSpace:String):void
		{
			clickSound.load(new URLRequest(soundClick));
			backSound.load(new URLRequest(soundBack));
			spaceSound.load(new URLRequest(soundSpace));
			
			isSound = true;
		}

		private function abcFnc(e:MouseEvent) 
		{
			addChar(e.currentTarget.charVal,0,0);
			keyDownAni(e.currentTarget.btn);
			shiftCapsOff();
			
			if ( isSound ) clickSound.play();
		}
		
		// change the key when it is pressed
		private function keyDownAni(mcName:MovieClip) {
			mcName.gotoAndStop(2);
			var theTimer:Timer = new Timer(300,1);
			theTimer.start();
			theTimer.addEventListener(TimerEvent.TIMER_COMPLETE, done);

			function done(e:TimerEvent):void {
				// moves back to the first frame
				mcName.gotoAndStop(1);
			}

		}

		// add EventListener and set the textfield characters in the movieclip
		private function setNumpadArray() {
			for (var i = 0; i < numpad_array.length; i++) {

				var dyn = keyboardMc["numpad" + numpad_array[i]];
				dyn.charVal = i;

				dyn.cTF.text = numpad_array[i];
				dyn.cTF.mouseEnabled = false;


				dyn.addEventListener(MouseEvent.MOUSE_DOWN, abcFnc);
			}
		}

		// add EventListener event and set the textfield characters in the movieclip
		private function setSpecialKeys() {
			for (var i = 0; i < specName_array.length; i++) {

				var dyn = keyboardMc["keyX" + specName_array[i]];
				dyn.charVal = specChar_array[i];
				dyn.cTF.text = specChar_array[i];
				dyn.cTF.mouseEnabled = false;


				dyn.addEventListener(MouseEvent.MOUSE_DOWN, abcFnc);
			}
		}
		// add EventListener event and set the textfield characters in the movieclip
		private function setNumKeys(numSwitch) {
			for (var i = 0; i < numName_array.length; i++) {
				var tempChar:String;
				if (numSwitch) {
					tempChar = numChar2_array[i];
				} else {
					tempChar = numChar1_array[i];
				}
				var dyn = keyboardMc["num" + numName_array[i]];

				dyn.cTF.text = tempChar;
				dyn.cTF.mouseEnabled = false;
				dyn.charVal = tempChar;

				dyn.addEventListener(MouseEvent.MOUSE_DOWN, abcFnc);
			}
		}

		// switch the CapsLock on/off
		private function switchCapsLock(e = null) {
			shiftCapsOff();
			if (capsOn) {
				keyboardMc.keyCaps.gotoAndStop(1);
				setAbcArray(false);
				setNumKeys(false);
				capsOn = false;
			} else {
				keyboardMc.keyCaps.gotoAndStop(2);
				setAbcArray(true);
				setNumKeys(true);
				capsOn = true;
			}
		}
		// switch Off capslock
		private function capsLockOff() {

			if (capsOn) {
				switchCapsLock();
			}
		}

		// switch the Shiftkey on/off
		private function shiftCapsSwitch(e = null) {
			capsLockOff();
			if (capsOnShift) {
				keyboardMc.keyShift1.gotoAndStop(1);
				keyboardMc.keyShift2.gotoAndStop(1);
				setAbcArray(false);
				setNumKeys(false);
				capsOnShift = false;
			} else {
				keyboardMc.keyShift1.gotoAndStop(2);
				keyboardMc.keyShift2.gotoAndStop(2);
				setAbcArray(true);
				setNumKeys(true);
				capsOnShift = true;
			}
		}
		// switch Off shiftKeys
		private function shiftCapsOff() {
			if (capsOnShift) {
				shiftCapsSwitch();
			}
		}

		// function listener for specialKeys 
		private function specialKeyAdd(e:MouseEvent) {
			keyDownAni(e.currentTarget.btn);
			addChar(e.target.parent.parent.charVal,e.target.parent.parent.from,e.target.parent.parent.to);
			
			if ( isSound )
			{
				if ( e.target.parent.parent.from == -1 && e.target.parent.parent.charVal == "" && e.target.parent.parent.to == 0 ) backSound.play();
				if ( e.target.parent.parent.from == 0 && e.target.parent.parent.charVal == " " && e.target.parent.parent.to == 0 ) spaceSound.play();
			}
		}

		// add EventListener events to the special function keys
		private function addSpecialKeys() {

			// backspace
			keyboardMc.backSpace.charVal = "";
			keyboardMc.backSpace.from = -1;
			keyboardMc.backSpace.to = 0;
			keyboardMc.backSpace.addEventListener(MouseEvent.MOUSE_DOWN, specialKeyAdd);

			// spacebar
			keyboardMc.keySpace.charVal = " ";
			keyboardMc.keySpace.from = 0;
			keyboardMc.keySpace.to = 0;
			keyboardMc.keySpace.addEventListener(MouseEvent.MOUSE_DOWN, specialKeyAdd);

			// delete key
			keyboardMc.keyDelete.charVal = "";
			keyboardMc.keyDelete.from = 0;
			keyboardMc.keyDelete.to = 1;
			keyboardMc.keyDelete.addEventListener(MouseEvent.MOUSE_DOWN, specialKeyAdd);

			// tab key
			keyboardMc.keyTab.charVal = "\t";
			keyboardMc.keyTab.from = 0;
			keyboardMc.keyTab.to = 0;
			keyboardMc.keyTab.addEventListener(MouseEvent.MOUSE_DOWN, specialKeyAdd);

			// enter key
			keyboardMc.keyEnter.charVal = "\n";
			keyboardMc.keyEnter.from = 0;
			keyboardMc.keyEnter.to = 0;
			keyboardMc.keyEnter.addEventListener(MouseEvent.MOUSE_DOWN, specialKeyAdd);

			// shift keys
			keyboardMc.keyShift1.addEventListener(MouseEvent.MOUSE_DOWN, shiftCapsSwitch);
			keyboardMc.keyShift2.addEventListener(MouseEvent.MOUSE_DOWN, shiftCapsSwitch);

			// caps lock
			keyboardMc.keyCaps.addEventListener(MouseEvent.MOUSE_DOWN, switchCapsLock);

		}


		// add the character to the texfield at a certain position
		private function addChar(c, iA, iB) {
			var t = currentTF.caretIndex;
			var beginC:Number = currentTF.selectionBeginIndex;
			var endC:Number = currentTF.selectionEndIndex;

			if (t < currentTF.maxChars || currentTF.maxChars == 0) {
				if (beginC == endC) {
					currentTF.replaceText(t+iA,t+iB,c);
				} else {
					currentTF.replaceText(beginC,endC,c);
				}
			}
		}
	}
}